import { login, getUserInfo, safeVerify, logout } from '@/api/user'
import {
  getToken,
  setToken,
  removeToken,
  getUser,
  setUser,
  removeUser
} from '@/utils/auth'
import { resetRouter } from '@/router'
const state = {
  token: getToken(),
  userInfo: getUser() ? JSON.parse(getUser()) : null,
  name: '',
  avatar: '',
  introduction: '',
  roles: []
}
const mutations = {
  SET_TOKEN: (state, token) => {
    state.token = token
    setToken(token)
  },
  SET_USERINFO: (state, userInfo) => {
    state.userInfo = userInfo
    setUser(JSON.stringify(userInfo))
  }
}
const actions = {
  login(store, userInfo) {
    const { commit } = store
    return new Promise((resolve, reject) => {
      login(userInfo).then(response => {
        if (response) {
          const { data } = response
          commit('SET_TOKEN', data.token)
          commit('SET_USERINFO', data)
          resolve(data)
          dispatch('permission/getRoutes', '', { root: true })
        } else {
          resolve(undefined)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  safeVerify(store, userInfo) {
    console.log('safeVerify')
    const { commit } = store
    return new Promise((resolve, reject) => {
      console.log('safeVerify----------', userInfo)
      safeVerify(userInfo).then(response => {
        console.log('safeVerify============')
        if (response) {
          const { data } = response
          commit('SET_TOKEN', data.token)
          commit('SET_USERINFO', data)
          resolve(data)
          dispatch('permission/getRoutes', '', { root: true })
        } else {
          resolve(undefined)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getUserInfo().then(res => {
        if (res) {
          commit('SET_USERINFO', res.data)
          resolve(res.data)
        }
      })
    })
  },
  // user logout
  logout({ commit, state, dispatch }) {
    return new Promise((resolve, reject) => {
      // logout().then(() => {
      commit('SET_TOKEN', '')
      commit('permission/RESET_ROUTES', '', { root: true })
      removeToken()
      removeUser()
      resetRouter()
      dispatch('tagsView/delAllViews', null, { root: true })
      resolve()
      // }).catch(err => {
      //   console.log(err)
      //   reject()
      // })
    })
  },
  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      commit('SET_TOKEN', '')
      removeToken()
      removeUser()
      resolve()
    })
  }
  // dynamically modify permissions
  // async changeRoles({ commit, dispatch }, role) {
  //   const token = role + '-token'
  //
  //   commit('SET_TOKEN', token)
  //   setToken(token)
  //
  //   const { roles } = await dispatch('getInfo')
  //
  //   resetRouter()
  //
  //   // generate accessible routes map based on roles
  //   const accessRoutes = await dispatch('permission/generateRoutes', roles, { root: true })
  //   // dynamically add accessible routes
  //   router.addRoutes(accessRoutes)
  //
  //   // reset visited views and cached views
  //   dispatch('tagsView/delAllViews', null, { root: true })
  // }
}
export default {
  namespaced: true,
  state,
  mutations,
  actions
}